/*
   Copyright 2009 Abdulla Abdurakhmanov (abdulla@latestbit.com)
   Original sources are available at www.latestbit.com

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

package org.latestbit.smartobjects.network.tcp.tests;

import static org.junit.Assert.*;

import java.io.IOException;
import java.net.URI;
import java.nio.ByteBuffer;

import org.junit.Test;
import org.latestbit.smartobjects.network.*;
import org.latestbit.smartobjects.network.tcp.*;

public class TCPFactoryTest {

	@Test
	public void testCreateAcceptor() throws Exception {
		INetworkFactory factory = new TCPFactory();
		URI serverURI = new URI("tcp://localhost:3333");
		IAcceptor acceptor = factory.createAcceptor(
				serverURI
		);
		
		acceptor.start();
		acceptor.stop();
	}

	@Test
	public void testCreateConnector() throws Exception {
		INetworkFactory factory = new TCPFactory();
		URI connectorURI = new URI("tcp://localhost:3333");
		IConnector connector = factory.createConnector(
				connectorURI
		);
		
		try {
			connector.connect();
			fail("Connect operation must throw exception");
		}
		catch(IOException ex) {
			
		}		
	}
	
	@Test
	public void testAcceptorListener() throws Exception {
		INetworkFactory factory = new TCPFactory();
		URI serverURI = new URI("tcp://localhost:3333");
		IAcceptor acceptor = factory.createAcceptor(
				serverURI
		);

		AcceptorListenerTester listener = new AcceptorListenerTester();
		acceptor.addListener(listener);
		acceptor.start();
		
		IConnector connector = factory.createConnector(
				serverURI
		);

		IConnector connector2 = factory.createConnector(
				serverURI
		);
		
		listener.getConditionLock().lock();
		connector.connect();
		listener.waitCondition();
		listener.getConditionLock().unlock();
		assertTrue( listener.getAcceptedConnections().size() == 1 );
		
		listener.getConditionLock().lock();
		connector2.connect();	
		listener.waitCondition();
		listener.getConditionLock().unlock();
		
		assertTrue( listener.getAcceptedConnections().size() == 2 );
		
		acceptor.stop();
		acceptor.delListener(listener);
	}
	
	@Test
	public void testConnectorListener() throws Exception {
		INetworkFactory factory = new TCPFactory();
		URI serverURI = new URI("tcp://localhost:3333");
		IAcceptor acceptor = factory.createAcceptor(
				serverURI
		);
		
		AcceptorListenerTester acceptorListener = new AcceptorListenerTester();
		acceptor.addListener(acceptorListener);
		acceptor.start();
		
		IConnector connector = factory.createConnector(
				serverURI
		);
		ConnectionListenerTester connectorListener = new ConnectionListenerTester();
		connector.addConnectionListener(connectorListener);
		
		connectorListener.getConditionLock().lock();
		acceptorListener.getConditionLock().lock();
		assertFalse( connectorListener.isConnected() );
		connector.connectAsync();
		connectorListener.waitCondition();
		acceptorListener.waitCondition();
		acceptorListener.getConditionLock().unlock();
		connectorListener.getConditionLock().unlock();
		assertTrue( connectorListener.isConnected() );
		assertTrue( acceptorListener.getAcceptedConnections().size() == 1 );
		
		// Disconnecting
		ConnectionListenerTester acceptorConnectionListener = new ConnectionListenerTester();
		acceptorConnectionListener.setConnected(true);
		acceptorConnectionListener.getConditionLock().lock();
		acceptorListener.getAcceptedConnections().iterator().next().addConnectionListener(acceptorConnectionListener);
		connector.close();
		acceptorConnectionListener.waitCondition();
		acceptorConnectionListener.getConditionLock().unlock();
		assertFalse ( acceptorConnectionListener.isConnected() );

		acceptor.stop();
	}
	
	
	@Test
	public void testExchangeData() throws Exception {
		INetworkFactory factory = new TCPFactory();
		URI serverURI = new URI("tcp://localhost:3333");
		IAcceptor acceptor = factory.createAcceptor(
				serverURI
		);
		
		AcceptorListenerTester acceptorListener = new AcceptorListenerTester();
		acceptor.addListener(acceptorListener);
		acceptor.start();
		
		IConnector connector = factory.createConnector(
				serverURI
		);
		ConnectionListenerTester connectorListener = new ConnectionListenerTester();
		connector.addConnectionListener(connectorListener);
		
		connectorListener.getConditionLock().lock();
		acceptorListener.getConditionLock().lock();
		assertFalse( connectorListener.isConnected() );
		connector.connectAsync();
		connectorListener.waitCondition();
		acceptorListener.waitCondition();
		acceptorListener.getConditionLock().unlock();
		connectorListener.getConditionLock().unlock();

		assertTrue( connectorListener.isConnected() );
		assertTrue( acceptorListener.getAcceptedConnections().size() == 1 );
		
		ConnectionListenerTester acceptorConnectionListener = new ConnectionListenerTester();
		acceptorListener.getAcceptedConnections().iterator().next().addConnectionListener(acceptorConnectionListener);
		acceptorListener.getAcceptedConnections().iterator().next().addStreamListener(acceptorConnectionListener);
		
		ByteBuffer buffer = ByteBuffer.allocate(5);
		buffer.put(new byte[] { 0x0A, 0x0B, 0x0C, 0x0D, 0x0E });
		buffer.flip();
		
		acceptorConnectionListener.getConditionLock().lock();
		connector.send(buffer);
		acceptorConnectionListener.waitCondition();
		acceptorConnectionListener.getConditionLock().unlock();
		
		buffer.flip();
		assertTrue ( buffer.equals(acceptorConnectionListener.getReceivedBuffer())); 
		

		acceptor.stop();
	}
}
